<?php
/**
 * 滤镜 过滤不正常言论
 */
class filterbadword implements ExtFilter{
	
	private $replace; //替换字符
	private $dict;	//字典
	private $folder; //当前目录
	private $filterData;
	const dictFilename = "dict.txt";
	
	public function __construct(){
		$this->replace = "*";
		$this->folder = getcwd().DIRECTORY_SEPARATOR."lib".DIRECTORY_SEPARATOR."class".DIRECTORY_SEPARATOR."filter".DIRECTORY_SEPARATOR;
		$tmpArray = _file($this->folder.self::dictFilename);
		for($i=0;$i<count($tmpArray);$i++){
			$this->dict[] = trim($tmpArray[$i]);
		}
	}
	
	public function todo(){
		_debug(array(__FILE__,__LINE__,'Filter Badword !Load Success!'));
		$this->execFilter(&$this->filterData);
	}
	
	private function execFilter(&$ars){
		//loop dict
		if(!is_array($ars))return;
		for($i=0;$i<count($this->dict);$i++)
		{
			$filterKey = $this->dict[$i];
			//loop variable
			foreach ($ars as $idx => $value){
				if('string' == gettype($value)){
					if(strpos($ars[$idx],$filterKey) !== false){
						$ars[$idx] = str_ireplace($filterKey,$this->replace,$ars[$idx]);
					}
				}
				elseif ('array' == gettype($value)){
					$this->execFilter($ars[$idx]);
				}
				elseif ('object' == gettype($value)){
					
				}
			}
		}
	}
	
	public function setData(&$data){
		$this->filterData = &$data;
	}
}

return  md5(__FILE__);